package personal.gavin.utils
{
	import mx.messaging.channels.StreamingAMFChannel;
	
	import personal.gavin.gtweet.widget.Profile;
	import personal.gavin.gtweet.widget.Setting;
	import personal.gavin.gtweet.widget.Status;
	
	public class UIUtil
	{
		public static function statusToHtml(_status:Status):String{
			var htmlText:String;
			htmlText = UIUtil.messEmail(_status.text);
			htmlText = UIUtil.markUrl(htmlText);
			htmlText = UIUtil.markReply(htmlText);
			htmlText = UIUtil.markHashtag(htmlText);
			htmlText = UIUtil.unMessEmail(htmlText);
			htmlText += "<br/>";
			htmlText = "<font size='12'>" + htmlText + "</font><br/>";
			
			htmlText += "<div align='right'><font size='10' color='#606060'>" + _status.time.toLocaleString() + "</font></div><br/>";
			var regExp:RegExp = new RegExp("(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)","g");
			if(regExp.test(_status.source)){
				htmlText += "<div align='right'><font size='10' color='#"+ Setting.globol_profile.profile_link_color +"'>via " + _status.source + "</font>";
			} else {
				htmlText += "<div align='right'><font size='10' color='#606060'>via " + _status.source + "</font>";
			}
			if(_status.in_reply_to_screen_name != ""){
				htmlText += "<font size='10' color='#606060'> in reply to " + _status.in_reply_to_screen_name + "</font></div>"
			}
			return htmlText;
		}
		public static function messEmail(str:String):String{
			var regExp:RegExp = /(\b[\w\.-]+)@([\w\.-]+\.\w{2,4}\b)/gi;
			var result:String = str.replace(regExp,"$1!==!$2");
			trace(result);
			return result;
		}
		public static function unMessEmail(str:String):String{
			var regExp:RegExp = /(\b[\w\.-]+)!==!([\w\.-]+\.\w{2,4}\b)/gi;
			var result:String = str.replace(regExp,"$1@$2");
			trace(result);
			return result;
		}
		public static function markUrl(str:String):String{
			var regExp:RegExp = new RegExp("(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)","g");
			var result:String = str.replace(regExp,"<font color='#" + Setting.globol_profile.profile_link_color +"'><a href='$&' target='_blank'>$&</a></font>");
			trace(result);
			return result;
		}
		public static function markReply(str:String):String{
			var regExp:RegExp = /@(\w+)/g;
			var result:String = str.replace(regExp,"<font color='#" + Setting.globol_profile.profile_link_color +"'><a href='http://twitter.fishnote.net/user.php?id=$1' target='_blank'>$&</a></font>");
			trace(result);
			return result;
		}
		public static function markHashtag(str:String):String {
			var regExp:RegExp = /[^(color=')]#(\w+)/g;
			var result:String = str.replace(regExp,"<font color='#" + Setting.globol_profile.profile_link_color +"'><a href='http://twitter.fishnote.net/search.php?q=$1' target='_blank'>$&</a></font>");
			trace(result);
			return result;
		}
		
		
		public static function profileToHtml(_profile:Profile):String{
			var htmlText:String;
			htmlText = "Bolg:"+UIUtil.markUrl(_profile.blog_url)+"<br/><br/>";
			var bioText:String = _profile.description;
			bioText = "Bio:" + UIUtil.messEmail(bioText);
			bioText = UIUtil.markUrl(bioText);
			bioText = UIUtil.markReply(bioText);
			bioText = UIUtil.markHashtag(bioText);
			bioText = UIUtil.unMessEmail(bioText) + "<br/><br/>";
			htmlText += bioText + "location:"+_profile.location;
			htmlText = "<font size='12'>" + htmlText + "</font>";
			return htmlText;
		}
	}
}